#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Created on 30/03/2010

@author: sebas
'''
class Book(object):
  '''
    Representa un libro
  '''
  def __init__(self, title, authors, gbook):
    self.title = title
    self.authors = authors
    self.gbook = gbook
    
  def __str__(self):
    return "%s - %s %s" % (self.title, " & ".join(map(lambda a : str(a), self.authors)), ("[G]" if self.gbook is not None else ""))

  def __eq__(self, other):
    return self.title == other.title and self.authors == other.authors

  def __cmp__(self, other):
    return cmp(self.title, other.title)

  def __hash__(self):
    return hash(self.title)

class Author(object):
  '''
    Representa un autor
  '''
  def __init__(self, name, sortname):
    self.name = name
    self.sortname = sortname
  
  def __str__(self):
    if self.sortname and self.sortname != self.name:
      return "%s (%s)" % (self.name, self.sortname)
    return self.name

  def __eq__(self, other):
    return self.name == other.name and self.sortname == other.sortname
  
  def __cmp__(self, other):
    return cmp(self.sortname, other.sortname)

  def __hash__(self):
    return hash(self.sortname)

class BookFile(object):
  '''
    Representa un libro y su ubicación física
  '''
  def __init__(self, book, location):
    self.book = book
    self.location = location

  def __str__(self):
    return self.book.__str__()

  def __eq__(self, other):
    return self.book == other.book

  def __cmp__(self, other):
    return cmp(self.book, other.book)

  def __hash__(self):
    return hash(self.book)

class Item(object):
  '''
    Representa un item de la base, en particular, un libro más atributos variables dependiendo el estadio de la normalización
  '''

  EDITED = "edited"

  def __init__(self, bookfile):
    self.bookfile = bookfile
    self.attributes = dict()

  def get_attribute(self, name):
    return self.attributes[name] if name in self.attributes.keys() else None

  def set_attribute(self, name, value):
    self.attributes[name] = value

  def __str__(self):
    return self.bookfile.book.__str__()

  def __eq__(self, other):
    return self.bookfile.book == other.bookfile.book
  
  def __cmp__(self, other):
    return cmp(self.bookfile, other.bookfile)

  def __hash__(self):
    return(self.bookfile)
